package com.bookstore.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter("/*")
public class MyEncodingFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        //1.设置POST请求中文乱码的问题
        //2.解决get请求的中文乱码问题
        HttpServletRequest hsr = (HttpServletRequest) request;
        if (hsr.getMethod().equalsIgnoreCase("post")) {
            request.setCharacterEncoding("UTF-8");
        }
            chain.doFilter(request, response);




    }
    @Override
    public void destroy () {
    }


}
